<template>
  <div class="system-user-container">
    <el-card shadow="hover">
      <div class="system-user-search mb15">
        <el-input clearable v-model="state.tableData.param.roleName" size="default" placeholder="请输入名称" style="max-width: 180px"  @clear="getRolePage"> </el-input>
        <el-button size="default" type="primary" class="ml10" @click="getRolePage">
          <el-icon>
            <ele-Search />
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onOpenAddRole">
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          新增
        </el-button>
      </div>
      <el-table :data="state.tableData.data" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column type="id" prop="id" label="ID" width="60" />
        <el-table-column prop="roleName" label="名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
        <!--        <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>-->
        <!--        <el-table-column prop="updateTime" label="更新时间" show-overflow-tooltip></el-table-column>-->
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenEditRole(scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
        :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum" background
        v-model:page-size="state.tableData.param.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="state.tableData.total">
      </el-pagination>
    </el-card>
    <RoleDialog @refresh="getRolePage()" ref="roleDialogRef" />
  </div>
</template>

<script setup name="tenantRole">
import { reactive, onMounted, ref, defineAsyncComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';

// 引入组件
const RoleDialog = defineAsyncComponent(() => import('/@/views/tenant/role/dialog.vue'));


import { useTenantApi } from "/@/api/tenant";

const tenantApi = useTenantApi()

const roleDialogRef = ref();
const state = reactive ({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      roleName: '',
      pageNum: 1,
      pageSize: 10,
    },
  },
});
const getRolePage = () => {
  let params = {
    "page": state.tableData.param.pageNum,
    "limit": state.tableData.param.pageSize,
    "roleName": state.tableData.param.roleName,
  }
  tenantApi.tenantRolePage(params).then(res => {
    state.tableData.data = res.data.list;
    state.tableData.total = res.data.totalCount;
  })
}
// 打开新增弹窗
const onOpenAddRole = () => {
  console.log(roleDialogRef);
  roleDialogRef.value.openDialog("add");
};
// 打开修改弹窗
const onOpenEditRole = (row) => {
  roleDialogRef.value.openDialog('edit',row.id);
};
// 删除用户
const onRowDel = (row) => {
  ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.roleName}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      tenantApi.tenantRoleDelete(row.id).then(() => {
        getRolePage();
        ElMessage.success('删除成功');
      })
    })
    .catch(() => { });
};
// 分页改变
const onHandleSizeChange = (val) => {
  state.tableData.param.pageSize = val;
  getRolePage();
};
// 分页改变
const onHandleCurrentChange = (val) => {
  state.tableData.param.pageNum = val;
  getRolePage()
};
// 页面加载时
onMounted(() => {
  getRolePage();
});
</script>
